<?php

/**
 * DATATABLE 分页查询, 配合 DATATABLE 视图使用
 *
 * @author xiemaomao@iwgame.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Message.php 929 2011-10-27 07:52:50Z mmxie $
 */
class IG_Controller_Action_Helper_DataTable extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @return array
     */
    public function getSortColumns(array $defaultSort = null)
    {
        $request = $this->getRequest();
        $iSortingCols = $request->getParam('iSortingCols');

        $sorts = array();
        /*
         * Ordering
         */
        if ( $request->getParam('iSortCol_0') !== null) {
            for ( $i=0 ; $i<intval( $request->getParam('iSortingCols') ) ; $i++ ) {
                $columnKey = intval( $request->getParam('iSortCol_' . $i));
                if ( $request->getParam('bSortable_' . $columnKey)
                     == "true" ) {
                    $sorts[] = $request->getParam('mDataProp_' . $columnKey) . ' '
                             . $request->getParam('sSortDir_' . $i);
                }
            }
        } else {
            return $defaultSort;
        }
        return $sorts;
    }

    /**
     *
     * @param  Zend_Paginator $paginator
     */
    public function direct(Zend_Paginator $paginator, array $header = null)
    {
        if ($displayLength = $this->getRequest()->getParam('iDisplayLength')) {
            $paginator->setDefaultItemCountPerPage($displayLength);
        }

        if ($displayStart = $this->getRequest()->getParam('iDisplayStart')) {
            $paginator->setCurrentPageNumber($displayStart/$displayLength+1);
        }

        $rows = $paginator->getCurrentItems()->toArray();
        if ($header) {
            $rows = $this->_convArray($rows, $header);
        }

        $this->getActionController()->getHelper('json')->direct(array(
            'sEcho' => $this->getRequest()->getParam('sEcho'),
            'iTotalRecords' => $paginator->getTotalItemCount(),
            'iTotalDisplayRecords' => $paginator->getTotalItemCount(),
            'aaData' => $rows
        ));
    }

    public function _convArray($rows, $columnsKey)
    {
        //先对空的进行过滤, 下一个循环会快一点
        foreach ($columnsKey as $key => $filters) {
            if (empty($filters)) {
                unset($columnsKey[$key]);
            }
        }

        foreach ($rows as $index => &$row) {
            foreach ($columnsKey as $key => $filters) {
                $row[$key] = $this->_filterRow($filters, $row, $key);
            }
        }
        return $rows;
    }

    /**
     * 过滤键定义:
     * - valueMap   array    键值对替换
     * - template   string   模板替换
     * - function   function 自定义函数处理,参数1: $row;参数2: $key;
     * - viewHelper string   视图助手
     *
     * @param array $filters
     * @param array $row
     * @param string $key
     * @return string
     */
    protected function _filterRow($filters, $row, $key)
    {
        $value = null;

        foreach ($filters as $filter => $data) {
            switch ($filter) {
                case 'valueMap':
                    if (isset($data[$row[$key]])) {
                        $value = $data[$row[$key]];
                    }
                break;
                case 'template':
                    $value = $this->_template($data, $row);
                break;
                case 'function':
                    $value = call_user_func($data, $row, $this);
                break;
                case 'viewHelper':
                    $value = $this->view->{$filter}($row[$key], $row);
                break;
                default:print_r($filters);exit;
                break;
            }
        }

        return $value;
    }
}